<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/7
 * Time: 下午2:48
 */

namespace App\Busi\Traits;


use App\Models\Company;
use App\Models\Dist;

Trait CompanyTrait
{
    public function getCompanyNameByID($id){
        if(!$id){
            return "";
        }
        $where = [
            ["id","=",$id],

        ];
        $field = ["id","name"];
        $data = Company::where($where)->select($field)->first();
        if($data){
            return "<a class='success' href='company/".$data['id']."' target='_blank'>{$data['name']}</a><br>";
        }else{
            return "";
        }
    }
    public function getCompanyNameByUID($uid){
        if(!$uid){
            return "";
        }
        $where = [
            ["uid","=",$uid],

        ];
        $field = ["id","name"];
        $data = Company::where($where)->select($field)->first();
        if($data){
            return "<a class='success' href='company/".$data['id']."' target='_blank'>{$data['name']}</a><br>";
        }else{
            return "";
        }
    }
    public function getCompanyList($page){
        $pageSize = 10;
        $where = [
            ["flag","=",3]
        ];
        $field = ["id","name","province_id","city_id","address","logo","star","view_num"];
        $data = Company::where($where)->select($field)->orderBy('show_order', 'asc')->paginate($pageSize);
        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["province_id"] = $this->getDistNameid($item["province_id"]);
            $item["city_id"] = $this->getDistNameid($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-1);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum"=>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];

        return $ret;
    }
    function getRecommendCompanyList($num){
        $where = [
            ["flag","=",3]
        ];
        $field = ["id","name","logo","pictures","star","view_num"];
        $data = Company::where($where)->limit($num)->orderBy("show_order","asc")->select($field)->get();
        $company_list = [];
        foreach ($data as $item){
            $company_item = [];
            $company_item['id'] = $item['id'];
            $company_item['name'] = $item['name'];
            $company_item['star'] = $item['star'];
            $company_item['view_num'] = $item['view_num'];
            $company_item['logo'] = config('admin.upload.host').$item['pictures']['0'];
            $company_item['logo_watermark'] = config('admin.upload.host').$item['logo'];

            array_push($company_list,$company_item);

        }
        return $company_list;
    }
    function getCompanyByUid($uid)
    {
        $company = Company::where('uid', $uid)->first();
        return $company;
    }
    public function getDistNameid($id){
        if(!$id){
            $dist_name = "中国";
            return $dist_name;
        }
        $where = [
            ['id','=',$id]
        ];
        $fields = ['dist_name'];
        $data = Dist::where($where)->select($fields)->first();
        if(!$data){
            $dist_name = "中国";
            return $dist_name;
        }
        $dist_name = $data['dist_name'];


        return $dist_name;
    }
    public function getCompanyListByDistId($distId){
        $pageSize = 10;
        $where = [
            ['city_id','=',$distId],
//            ["flag","=",3]
        ];
        $field = ["id","name","province_id","city_id","address","logo","star","view_num"];
        $data = Company::where($where)->select($field)->orderBy('show_order', 'asc')->paginate($pageSize);

        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["province_id"] = $this->getDistNameid($item["province_id"]);
            $item["city_id"] = $this->getDistNameid($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-1);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum"=>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];
        return $ret;
    }
    public function getCompanyListByCompanyIds($company_ids){
        $pageSize = 10;

        $field = ["id","name","province_id","city_id","address","logo","star","view_num"];
        $data = Company::wherein('id',$company_ids)->select($field)->orderBy('show_order', 'asc')->paginate($pageSize);

        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["province_id"] = $this->getDistNameid($item["province_id"]);
            $item["city_id"] = $this->getDistNameid($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-1);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum"=>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];
        return $ret;
    }
    public function getCompanyListByTag($tag){
        switch ($tag){
            case "multi": $data= $this->getCompanyListByMulti();break;
            case "view": $data= $this->getCompanyListByView();break;
            case "star": $data= $this->getCompanyListByStar();break;
            default:$data= $this->getCompanyListByMulti();break;

        }
        return $data;

    }
    //综合排序
    public function getCompanyListByMulti(){
        $pageSize = 10;
        $where = [
//            ["flag","=",3]
        ];
        $field = ["id","name","province_id","city_id","address","logo","star","view_num"];
        $data = Company::where($where)->select($field)
            ->orderBy('show_order', 'asc')
            ->orderBy('view_num','desc')
            ->orderBy('star','desc')
            ->paginate($pageSize);

        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["province_id"] = $this->getDistNameid($item["province_id"]);
            $item["city_id"] = $this->getDistNameid($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-2);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum"=>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];
        return $ret;
    }
    public function getCompanyListByView(){
        $pageSize = 10;
        $where = [
//            ["flag","=",3]
        ];
        $field = ["id","name","province_id","city_id","address","logo","star","view_num"];
        $data = Company::where($where)->select($field)
            ->orderBy('view_num','desc')
            ->paginate($pageSize);

        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["province_id"] = $this->getDistNameid($item["province_id"]);
            $item["city_id"] = $this->getDistNameid($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-2);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum"=>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];
        return $ret;
    }
    public function getCompanyListByStar(){
        $pageSize = 10;
        $where = [
//            ["flag","=",3]
        ];
        $field = ["id","name","province_id","city_id","address","logo","star","view_num"];
        $data = Company::where($where)->select($field)
            ->orderBy('star','desc')
            ->paginate($pageSize);

        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["province_id"] = $this->getDistNameid($item["province_id"]);
            $item["city_id"] = $this->getDistNameid($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-2);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum"=>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];
        return $ret;
    }
}